﻿namespace NJQuery {
	using System;
	using System.ComponentModel;
	using System.ComponentModel.Design.Serialization;
	using System.Globalization;
	using System.Reflection;
	using System.Collections;

	/// <summary>
	/// Provides conversions for the <see cref="NumberOfMonths"/> struct.
	/// </summary>
	public class NumberOfMonthsConverter : TypeConverter {


		#region Static Methods

		private static InstanceDescriptor GetInstanceDescriptor(NumberOfMonths value) {

			if(value.IsSingle) return new InstanceDescriptor(
				typeof(NumberOfMonths).GetField("Single", BindingFlags.Public | BindingFlags.Static),
				null, true);

			if(value.IsMatrix) return new InstanceDescriptor(
				typeof(NumberOfMonths).GetConstructor(new Type[] { typeof(int), typeof(int) }),
				new object[] { value.Rows, value.Columns });

			return new InstanceDescriptor(
				typeof(NumberOfMonths).GetConstructor(new Type[] { typeof(int) }),
				new object[] { value.Count });
		}

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new <see cref="NumberOfMonthsConverter"/> class.
		/// </summary>
		public NumberOfMonthsConverter() { }

		#endregion


		#region Methods: Capabilities

		/// <summary>
		/// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
		/// </summary>
		/// <param name="context">A <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
		/// <param name="sourceType">The type of the object to be converted.</param>
		/// <returns>Whether or not the conversion can be performed.</returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
			if(sourceType == typeof(string)) return true;
			return base.CanConvertFrom(context, sourceType);
		}

		/// <summary>
		/// Returns whether this converter can convert the object to the specified type, using the specified context.
		/// </summary>
		/// <param name="context">A <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
		/// <param name="destinationType">The target type of the object.</param>
		/// <returns>Whether or not the conversion can be performed.</returns>
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
			if(destinationType == typeof(string)) return true;
			if(destinationType == typeof(InstanceDescriptor)) return true;
			return base.CanConvertTo(context, destinationType);
		}

		#endregion


		#region Methods: Perform tasks

		/// <summary>
		/// Converts the given object to the type of this converter, using the specified context and culture information.
		/// </summary>
		/// <param name="context">A <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
		/// <param name="culture">The <see cref="CultureInfo"/> to use as the current culture.</param>
		/// <param name="value">The object to be converted</param>
		/// <returns>The converted object.</returns>
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {

			if(value == null) return new NumberOfMonths();

			string text = value as string;
			if(text != null) {
				NumberOfMonths numberOfMonths;
				if(NumberOfMonths.TryParse(text, out numberOfMonths)) return numberOfMonths;
				return null;
			}

			return base.ConvertFrom(context, culture, value);
		}

		/// <summary>
		/// Converts the given object value to the specified type, using the specified context and culture information.
		/// </summary>
		/// <param name="context">A <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
		/// <param name="culture">The <see cref="CultureInfo"/> to use as the current culture.</param>
		/// <param name="value">The object to be converted</param>
		/// <param name="destinationType">The <see cref="Type"/> to convert the value parameter to.</param>
		/// <returns>The converted object.</returns>
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {

			if(destinationType == typeof(string))
				return ((NumberOfMonths)value).ToString();

			if(destinationType == typeof(InstanceDescriptor))
				return GetInstanceDescriptor((NumberOfMonths)value);

			return base.ConvertTo(context, culture, value, destinationType);
		}

		#endregion


	}

}